$(document).ready(function(){
	var date=new Date;
	var year=date.getFullYear();
	$('#nowyear').val(year);
	getdis();
})

var options = {
	title: {
        text: '箱量统计'
    },
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
        }
    },
    legend: {
        data:['箱量']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        data: []
    },
    yAxis: {
    },
    series: 
	[ {
		name : '箱量',
		type : 'line',
		data : []
	}]
 };


var xlChart = echarts.init(document.getElementById('xldiv'));
xlChart.setOption(options);
load("/statistics/zxlStatistics.action?tjflag=1", xlChart);

/***加载数据***/
function load(url, myChart){
    $.get(mainDomain + url).done(function (data) {
    	comparetb();
    	
        var firstxlxx = [];
        var secondxlxx = [];
        
        $.each(data.categories, function(n, item){
        	firstxlxx.push(0);
        	secondxlxx.push(0);
        })
        
        var replaceindex;
        $.each(data.firstlist, function(i, entry){
            if("A" == entry.type){
            	replaceindex = $.inArray(entry.df,data.categories);
            	if(replaceindex!=-1){
            		firstxlxx.splice(replaceindex,1,entry.count);
            	}
                return;
            }
        });
        $.each(data.secondlist, function(i, entry){
            if("A" == entry.type){
            	replaceindex = $.inArray(entry.df,data.categories);
            	if(replaceindex!=-1){
            		secondxlxx.splice(replaceindex,1,entry.count);
            	}
                return;
            }
        });
        
        // 填入数据
        myChart.setOption({
            xAxis: {
                data: data.categories
            },
            yAxis : 
            	[ {
				name : '箱量(TEU)'
			}],
			legend: {
		        data: data.yearlist,
		        x: "center",
                y: "top",
                itemGap: 20
		    },
            series: 
            	[ {
				name : data.yearlist[0],
				type : 'bar',
				barWidth: '25%',
				data : firstxlxx
			} ,{
				name : data.yearlist[1],
				type : 'bar',
				barWidth: '25%',
				data : secondxlxx
			}]
        });
    });
}

function doSearch(){
	var nowyear = $('#nowyear').val();
	var searchurl = "/statistics/zxlStatistics.action?tjflag=1";
	if(nowyear!=null&&nowyear!=''){
		searchurl += '&nowyear=' + nowyear;
		load(searchurl, xlChart);
	}else{
		load(searchurl, xlChart);
	}
}

function comparetb(){
	var nowyear = $('#nowyear').val();
	$('#lasttdname').html(nowyear-1);
	$('#nowtdname').html(nowyear);
	$.ajax( {  
        type : 'POST',  
        url : mainDomain + '/statistics/xlStatistics.action',
        data : 'nowyear='+nowyear+'&tjflag='+1,
        dataType: "json",
        async: false,
        success : function(data) {
        	var xlxx = [];
            
            $.each(data.categories, function(n, item){
            	xlxx.push(0);
            });
            
            var replaceindex;
	        $.each(data.list, function(i, entry){
	            if("A" == entry.type){
	            	replaceindex = $.inArray(entry.df,data.categories);
	            	if(replaceindex!=-1){
	            		xlxx.splice(replaceindex,1,entry.count);
	            	}
	                return;
	            }
	        });
        	
        	$('#nowone').html(xlxx[0]);
        	$('#nowtwo').html(xlxx[1]);
        	$('#nowthree').html(xlxx[2]);
        	$('#nowfour').html(xlxx[3]);
        	$('#nowtotal').html(xlxx[0]+xlxx[1]+xlxx[2]+xlxx[3]);
		}
      });
	
	$.ajax( {  
		type : 'POST',  
        url : mainDomain + '/statistics/xlStatistics.action',
        data : 'nowyear='+(nowyear-1)+'&tjflag='+1,
        dataType: "json",
        async: false,
        success : function(data) {
        	var xlxx = [];
            
            $.each(data.categories, function(n, item){
            	xlxx.push(0);
            });
            
            var replaceindex;
	        $.each(data.list, function(i, entry){
	            if("A" == entry.type){
	            	replaceindex = $.inArray(entry.df,data.categories);
	            	if(replaceindex!=-1){
	            		xlxx.splice(replaceindex,1,entry.count);
	            	}
	                return;
	            }
	        });
        	
        	$('#lastone').html(xlxx[0]);
        	$('#lasttwo').html(xlxx[1]);
        	$('#lastthree').html(xlxx[2]);
        	$('#lastfour').html(xlxx[3]);
        	$('#lasttotal').html(xlxx[0]+xlxx[1]+xlxx[2]+xlxx[3]);
		}
      });
	
	//第一季度同比
	$('#tbone').html(cop($('#lastone').text(),$('#nowone').text()));
	//第二季度同比
	$('#tbtwo').html(cop($('#lasttwo').text(),$('#nowtwo').text()));
	//第三季度同比
	$('#tbthree').html(cop($('#lastthree').text(),$('#nowthree').text()));
	//第四季度同比
	$('#tbfour').html(cop($('#lastfour').text(),$('#nowfour').text()));
	//总计同比
	$('#tbtotal').html(cop($('#lasttotal').text(),$('#nowtotal').text()));
}

function cop(last,now){
	var percent;
	if(now=="0"){
		percent = '无';
	}else{
		percent = Math.floor((now-last)/now*100)+'%';
	}
	return percent;
}

function getdis(){
	$.ajax( {  
		type : 'POST',  
        url : mainDomain + '/zy/comboxlist.action',
        data : 'sfsc=0',
        dataType: "json",
        async: false,
        success : function(data) {
        	if(data.length>0){
        		$('#disdiv').html(data[0].zylx);
        	}
        }
      });
}